package com.spzx.product.controller;

import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductSpec;
import com.spzx.product.service.ProductSpecService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;

import java.util.Arrays;


/**
 * <p>
 * 商品规格 前端控制器
 * </p>
 *
 * @author Haoran Sun
 * @since 2024-07-15
 */
@RestController
@RequestMapping("/productSpec")
@Tag(name = "商品规格的信息管理")
public class ProductSpecController extends BaseController {

    @Resource
    private ProductSpecService productSpecService;

    /**
     * 根据条件分页查询商品规格信息
     * @param productSpec 查询条件
     * @return 分页数据
     */
    @GetMapping("/list")
    @Operation(summary = "分页查询商品规格信息")
    public TableDataInfo list(ProductSpec productSpec) {
        startPage();
        return getDataTable(productSpecService.listProductSpecByQuery(productSpec));
    }

    /**
     * 获取商品规格详细信息
     * @param id 商品规格id
     * @return 商品规格的详细信息
     */
    @Operation(summary = "根据id查询商品规格信息")
    @GetMapping("/productSpec/{id}")
    public AjaxResult getInfoById(@PathVariable("id") Long id){
        return AjaxResult.success(productSpecService.getInfoById(id));
    }

    /**
     * 新增商品规格信息
     *
     * @param productSpec 商品规格信息
     * @return 修改结果
     */
    @PostMapping
    @Operation(summary = "新增商品规格信息")
    public AjaxResult add(@RequestBody @Validated ProductSpec productSpec) {
        return toAjax(productSpecService.save(productSpec));
    }

    /**
     * 修改商品规格信息
     * @param productSpec 商品规格信息
     * @return 修改结果
     */
    @PutMapping
    @Operation(summary = "修改商品规格信息")
    public AjaxResult edit(@RequestBody @Validated ProductSpec productSpec) {
        return toAjax(productSpecService.updateById(productSpec));
    }

    /**
     * 删除商品规格信息
     * @param ids 要删除的商品规格id集合
     * @return 删除结果
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除商品规格信息")
    public AjaxResult remove(@PathVariable("ids") Long[] ids) {
        return toAjax(productSpecService.removeBatchByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "根据分类id查询商品规格列表")
    @GetMapping("productSpecList/{categoryId}")
    public AjaxResult getProductSpecListById(@PathVariable("categoryId") Long categoryId) {
        return success(productSpecService.getProductSpecListById(categoryId));
    }







}
